package com.miaosha.redis.set;

import redis.clients.jedis.Jedis;

import java.util.Map;

/**
 * 博客案例
 *
 * @author liushiqiao
 * @create 2021-10-09 7:54
 **/
public class BlogTag {

    private Jedis jedis = new Jedis("182.42.113.98", 6379);

    /**
     * 发布博客
     *
     * @param id
     * @param blog
     * @param tags
     * @return
     */
    public boolean publishBlog(long id, Map<String, String> blog, String[] tags) {

        if (jedis.hexists("article::" + id, "title")) {
            return false;
        }

        blog.put("content_length", String.valueOf(blog.get("content").length()));
        jedis.hmset("article::" + id, blog);
        jedis.lpush("log_list", String.valueOf(id));

        jedis.sadd("article::" + id + "::tags", tags);
        return true;

    }

    /**
     * 获取博客
     *
     * @param id
     * @return
     */
    public Map<String, String> findById(long id) {
        Map<String, String> blog = jedis.hgetAll("article::" + id);
        jedis.incr("article:" + id + ":view_count");
        return blog;
    }

    /**
     * 更新博客
     *
     * @param id
     * @param blog
     */
    public void updateBlog(long id, Map<String, String> blog) {
        jedis.hmset("article:" + id, blog);
    }

    /**
     * 预览
     *
     * @param id
     * @return
     */
    public String previewBlog(long id) {
        return jedis.getrange("article:" + id + ":content", 0, 10);
    }


    /**
     * 对博客进行点赞
     *
     * @param id
     */
    public void likeBlog(long id) {
        jedis.incr("article:" + id + ":like_count");
    }
}
